package com.itany.corejava.code08_集合.set集合;

/**
 * @author MissChen
 * @date 2024年03月05日 11:11
 */

import java.util.HashSet;
import java.util.Objects;

/**
 * 定义一个学生类，有no、name
 * 向HashSet中添加2个学生实例
 *
 */
public class Test02_判断元素是否重复 {
    public static void main(String[] args) {
        HashSet<Student> students=new HashSet<>();
        Student stu1=new Student(1,"alice");
        Student stu2=new Student(1,"alice");
        students.add(stu1);
        students.add(stu2);
        System.out.println(stu1.hashCode());
        System.out.println(stu2.hashCode());
        System.out.println(students);

        HashSet<String> set=new HashSet<>();
        set.add("a");
        set.add("a");
        System.out.println(set);
    }
}
class Student{
    Integer no;
    String name;

    public Student(Integer no, String name) {
        this.no = no;
        this.name = name;
    }

    public Integer getNo() {
        return no;
    }

    public void setNo(Integer no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(no, student.no) &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(no, name);
    }

    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}